package com.newzqxq.koalamoney.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.common.Params;
import com.newzqxq.koalamoney.entity.RegisterBean;
import com.newzqxq.koalamoney.presenter.LoginPresenter;
import com.newzqxq.koalamoney.utils.AppUtils;
import com.newzqxq.koalamoney.utils.MyToast;
import com.newzqxq.koalamoney.view.ILoginView;
import com.newzqxq.koalamoney.widget.ClearWriteEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ZQXQ-Developer on 2017/7/13.
 */

public class LoginActivity extends BaseActivity implements ILoginView {
    protected Context mContext;
    @BindView(R.id.title_mid_tv)
    TextView tvTitle;

    @BindView(R.id.de_login_phone)
    ClearWriteEditText mPhoneEdit;//电话号码
    @BindView(R.id.de_login_password)
    ClearWriteEditText mPasswordEdit;//密码

    private LoginPresenter loginPresenter;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    @Override
    protected int getContentViewId() {
        return R.layout.ui_login;
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this);
        loginPresenter.start();
    }

    @Override
    protected void initView() {
        mContext = this;
        sp = getSharedPreferences(Params.CONFIG, MODE_PRIVATE);
        editor = sp.edit();
        tvTitle.setText(R.string.ui_login);

        mPhoneEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.length() == 11) {
                    AppUtils.onInactive(mContext, mPhoneEdit);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        String oldPhone = sp.getString(Params.PHONE, "");
        String oldPassword = sp.getString(Params.PAWD, "");

        if (!TextUtils.isEmpty(oldPhone) && !TextUtils.isEmpty(oldPassword)) {
            mPhoneEdit.setText(oldPhone);
            mPasswordEdit.setText(oldPassword);
        }
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {

    }

    @OnClick({R.id.tvRegister, R.id.de_login_sign, R.id.de_login_forgot})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.tvRegister:
                startActivityForResult(new Intent(this, RegiserActivity.class), 1);
                break;
            case R.id.de_login_forgot:
                startActivityForResult(new Intent(this, ForgetPasswordActivity.class), 2);
                break;
            case R.id.de_login_sign://登录
                String phoneString = mPhoneEdit.getText().toString().trim();
                String passwordString = mPasswordEdit.getText().toString().trim();
                if (TextUtils.isEmpty(phoneString)) {
                    MyToast.shortToast(mContext, R.string.phone_number_is_null);
                    mPhoneEdit.setShakeAnimation();
                    return;
                }

                if (TextUtils.isEmpty(passwordString)) {
                    MyToast.shortToast(mContext, R.string.password_is_null);
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                if (passwordString.contains(" ")) {
                    MyToast.shortToast(mContext, R.string.password_cannot_contain_spaces);
                    mPasswordEdit.setShakeAnimation();
                    return;
                }
                editor.putBoolean(Params.EXIT, false);
                editor.apply();
                loginPresenter.login(getPhone(), getPawd());
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2 && data != null) {
            String phone = data.getStringExtra("phone");
            String password = data.getStringExtra("password");
            mPhoneEdit.setText(phone);
            mPasswordEdit.setText(password);
        } else if (data != null && requestCode == 1) {
            String phone = data.getStringExtra("phone");
            String password = data.getStringExtra("password");
            if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)) {
                mPhoneEdit.setText(phone);
                mPasswordEdit.setText(password);
                editor.putString(Params.PHONE, phone);
                editor.putString(Params.PAWD, password);
                editor.apply();
            }

        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public String getPhone() {
        return mPhoneEdit.getText().toString().trim();
    }

    @Override
    public String getPawd() {
        return mPasswordEdit.getText().toString().trim();
    }

    @Override
    public void onLoginResult(RegisterBean.DataBean data) {
        editor.putString(Params.PHONE, getPhone());
        editor.putString(Params.PAWD, getPawd());
        editor.apply();
        startActivity(new Intent(this, CreateGestureActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.end();
    }
}
